<?php
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl;
global $dbtables, $color_header, $color_line1, $color_line2;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	die();

//-------------------------------------------------------------------------------------------------

// Get the POST and GET variables
$planets = (isset($_GET['planets'])) ? $_GET['planets'] : '';
$buildp = (isset($_GET['buildp'])) ? $_GET['buildp'] : '';
$builds = (isset($_GET['builds'])) ? $_GET['builds'] : '';
$sellid = (isset($_GET['sellid'])) ? $_GET['sellid'] : '';
$planet_name = (isset($_GET['planet_name'])) ? $_GET['planet_name'] : '';
$planet_id = (isset($_GET['planet_id'])) ? $_GET['planet_id'] : '';

if (!empty($buildp) && !empty($builds))
	go_build_base($buildp,$builds);		
elseif (!empty($planets)) 
	collect_credits($planets);
elseif (!empty($sellid))
	ToggleSelling($sellid);
elseif (!empty($planet_name))
	RenamePlanet($planet_name,$planet_id);
else
	change_planet_production();

function go_build_base($planet_id, $sector_id)
{
	global $smcFunc, $txt, $scripturl, $user_info;
	global $dbtables;
	global $base_ore, $base_organics, $base_goods, $base_credits;
	
	BigTitle($txt['bnt_pr_title']);
	
	// Pull player info into $playerinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	// Pull sector info into $sectorinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_universe}
		WHERE sector_id = {int:sector_id}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector_id' => $playerinfo['sector']
		)
	);
	$sectorinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	// Pull planet information into $planetinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}
		WHERE planet_id = {int:planet_id}',
		array(
			'db_planets' => $dbtables['planets'],
			'planet_id' => $planet_id
		)
	);
	if ($smcFunc['db_num_rows']($result) > 0)
		$planetinfo = $smcFunc['db_fetch_assoc']($result);
	else
		$planetinfo = '';
	$smcFunc['db_free_result']($result);

	Real_Space_Move($sector_id, $playerinfo);

	// Build a base.
	if ($planetinfo['ore'] >= $base_ore && $planetinfo['organics'] >= $base_organics && $planetinfo['goods'] >= $base_goods && $planetinfo['credits'] >= $base_credits)
	{
		// ** Create The Base
		$result = $smcFunc['db_query']('', '
			UPDATE {raw:db_planets}
			SET base = "Y",
				ore = {int:planetinfo_ore}  - {int:base_ore},
				organics = {int:planetinfo_organics} - {int:base_organics},
				goods = {int:planetinfo_goods}  - {int:base_goods},
				credits = {int:planetinfo_credits}  - {int:base_credits} 
			WHERE planet_id = {int:planet_id}',
			array(
				'db_planets' => $dbtables['planets'],
				'planet_id' => $planet_id,
				'planetinfo_ore' => $planetinfo['ore'],
				'base_ore' => $base_ore,
				'planetinfo_organics' => $planetinfo['organics'],
				'base_organics' => $base_organics,
				'planetinfo_goods' => $planetinfo['goods'],
				'base_goods' => $base_goods,
				'planetinfo_credits' => $planetinfo['credits'],
				'base_credits' => $base_credits,
			)
		);
		
		// Update user turns.
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET turns = turns - 1,
				turns_used = turns_used + 1
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
		
		// Refresh planet information.
		$result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_planets}
			WHERE planet_id = {int:planet_id}',
			array(
				'db_planets' => $dbtables['planets'],
				'planet_id' => $planet_id
			)
		);
		$planetinfo = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		
		// Notify user of base results.
		echo $txt['bnt_planet_bbuild'], '<br><br>';
		
		// Calculate ownership and notify user ff results
		$ownership = calc_ownership($playerinfo['sector']);
		
		if (!empty($ownership))
			echo '<p>', $ownership, '</p>';
		
		// Get updated player turns
		$result = $smcFunc['db_query']('', '
			SELECT turns, turns_used
			FROM {raw:db_ships}
			WHERE user_id = {int:user_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'user_id' => $user_info['id'],
			)
		);
		$playerinfo = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		
		// Echo the turns to hidden input for display update
		echo '	<input id="build_base_turns" type="hidden" value="', $playerinfo['turns'], '">
				<input id="build_base_turns_used" type="hidden" value="', $playerinfo['turns_used'], '">';
	}
}

function collect_credits($planets)
{
	global $smcFunc, $txt, $scripturl, $user_info;
	global $dbtables;
	
	BigTitle($txt['bnt_pr_title']);
	
	// Pull player info into $playerinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	$credits_start = $playerinfo['credits'];
	$turns_start = $playerinfo['turns_used'];
	$current_state = "GO";
	
	$planetarray = explode(",", $planets);
	
	// Create an array of sector -> planet pairs
	for ($i = 0; $i < count($planetarray); $i++)
	{
		$result = $smcFunc['db_query']('', '
			SELECT sector_id
			FROM {raw:db_planets}
			WHERE planet_id = {int:planet_id}',
			array(
				'db_planets' => $dbtables['planets'],
				'planet_id' => $planetarray[$i]
			)
		);
		$planetinfo = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		$sector_planet_pair[$i]= array($planetinfo['sector_id'], $planetarray[$i]);
	}

	// Sort the array so that it is in order of sectors, lowest number first, not closest.
	sort($sector_planet_pair);
	reset($sector_planet_pair);

	echo '
				<div id="collect_credits_result_frame">
					<div class="overlay"></div>
					<div id="collect_credits_result_content">
						<h4>', $txt['bnt_pr_colcredits_result'], '</h4>';
	
	// Run through the list of sector planet pairs realspace moving to each sector and then performing the transfer. 
	// Based on the way realspace works we don't need a sub loop -- might add a subloop to clean things up later.
	for ($i = 0; $i < count($planetarray) && $current_state == "GO"; $i++)
	{
		$current_state = Real_Space_Move($sector_planet_pair[$i][0], $playerinfo);
		
		// Skip if hostile and go to the next
		if ($current_state == 'HOSTILE')
			$current_state = "GO";
		elseif ($current_state == 'GO')
			$current_state = Take_Credits($sector_planet_pair[$i][0], $sector_planet_pair[$i][1]);
		else
			echo '<h4>', $txt['bnt_pr_noturns'], '</h4>';
	}

	if ($current_state != "GO" && $current_state != "HOSTILE")
		echo '<p>', $txt['bnt_pr_noturns'], '</p>';
	
	$result = $smcFunc['db_query']('', '
		SELECT turns_used, turns, credits
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	$credits_collected = $playerinfo['credits'] - $credits_start;
	$turns_used = $playerinfo['turns_used'] - $turns_start;
	
	$credits_text = str_replace("[credits]", Number($credits_collected), $txt['bnt_pr_colcredits_earned']);
	$turns_text = str_replace("[turns]", Number($turns_used), $txt['bnt_pr_colcredits_turns']);
	
	// Write turn and credit info, javascript will pick this up and update display.
	echo '				
						<h4>', $credits_text , '</h4>
						<h4>', $turns_text , '</h4>
						<input id="collect_credits_turns_used" type="hidden" value="', Number($playerinfo['turns_used']) ,'" />
						<input id="collect_credits_turns_available" type="hidden" value="', Number($playerinfo['turns']) ,'" />
						<input id="collect_credits_balance" type="hidden" value="', Number($playerinfo['credits']) ,'" />
					</div>
				</div>';
}

function Take_Credits($sector_id, $planet_id)
{
	global $smcFunc, $txt, $scripturl, $user_info;
	global $dbtables;
	
	// Update player info into $playerinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	// Pull planet information into $planetinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}
		WHERE planet_id = {int:planet_id}',
		array(
			'db_planets' => $dbtables['planets'],
			'planet_id' => $planet_id
		)
	);
	$planetinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	// Set the name for unamed planets to be "unnamed".
	if (empty($planetinfo['name']))
		$planet['name'] = $txt['bnt_unnamed'];

	// Verify player is still in same sector as the planet.
	if ($playerinfo['sector'] == $planetinfo['sector_id'])
	{
		if ($playerinfo['turns'] >= 1)
		{
			// Verify player owns the planet to take credits from.
			if ($planetinfo['owner'] == $playerinfo['ship_id'])
			{
		        // Get number of credits from the planet and current number player has on ship.
		        $CreditsTaken = $planetinfo['credits'];
		        $CreditsOnShip = $playerinfo['credits'];
		        $NewShipCredits = $CreditsTaken + $CreditsOnShip;

				// Update the planet record for credits
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_planets}
					SET credits = 0
					WHERE planet_id = {int:planet_id}',
					array(
						'db_planets' => $dbtables['planets'],
						'planet_id' => $planetinfo['planet_id']
					)
				);
				
		        // Update the player ships credits and turns
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_ships}
					SET credits = {float:NewShipCredits},
						turns = turns - 1
					WHERE ship_id = {int:ship_id}',
					array(
						'db_ships' => $dbtables['ships'],
						'NewShipCredits' => round($NewShipCredits),
						'ship_id' => $playerinfo['ship_id']
					)
				);
				
				// Update player credit total
				$result = $smcFunc['db_query']('', '
					SELECT credits
					FROM {raw:db_ships}
					WHERE ship_id = {int:ship_id}',
					array(
						'db_ships' => $dbtables['ships'],
						'ship_id' => $playerinfo['ship_id']
					)
				);
				$shipcredits = $smcFunc['db_fetch_assoc']($result);
				$smcFunc['db_free_result']($result);
				
				$text = str_replace("[credits_taken]", NUMBER($CreditsTaken), $txt['bnt_pr_tookcredits']);
				$text = str_replace("[planet_name]", $planetinfo['name'], $text);
				echo '<p>', $text, '</p>';
				
				$retval = "GO";
			}
			else
			{
				$text = str_replace("[tdr_source_name]", $planetinfo['name'], $txt['bnt_tdr_notyourplanet']);
				$text = str_replace("[tdr_source_sector_id]", $planetinfo['sector_id'], $text);
				echo '<p>', $text, '</p>';
				$retval = "GO";
			}
		}
		else
		{
			$text = str_replace("[planet_name]", $planetinfo['name'], $txt['bnt_pr_noturns2']);
			$text = str_replace("[sector_id]", $planetinfo['sector_id'], $text);
			echo '<p>', $text, '</p>';
			$retval = "BREAK";
		}
	}
	else
	{
		echo '<p>', $text, '</p>';
		$retval = "BREAK";
	}
	return($retval);
}

function change_planet_production()
{
	global $smcFunc, $txt, $scripturl, $user_info;
	global $dbtables;
	global $default_prod_ore, $default_prod_organics, $default_prod_goods, $default_prod_energy;
	global $default_prod_fighters, $default_prod_torp;
	
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	/*
		Check for and get updated global production values.
		This may not be set if we are changing production values from the planet screen
	*/
	if (isset($_GET['global_production_changes']))
	{
		$global_production_string = $_GET['global_production_changes'];
		$global_production = explode(",", $global_production_string);
		$global_ore = $global_production[0];
		$global_organics = $global_production[1];
		$global_goods = $global_production[2];
		$global_energy = $global_production[3];
		$global_fighters = $global_production[4];
		$global_torps = $global_production[5];
		
		$smcFunc['db_query']('','
			UPDATE {raw:db_ships}
			SET global_ore = {int:global_ore},
				global_organics = {int:global_organics},
				global_goods = {int:global_goods},
				global_energy = {int:global_energy},
				global_fighters = {int:global_fighters},
				global_torps = {int:global_torps}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id'],
				'global_ore' => $global_ore,
				'global_organics' => $global_organics,
				'global_goods' => $global_goods,
				'global_energy' => $global_energy,
				'global_fighters' => $global_fighters,
				'global_torps' => $global_torps,
			)
		);
	}
	// Otherwise use the current global production values from the players settings
	else
	{
		$global_ore = $playerinfo['global_ore'];
		$global_organics = $playerinfo['global_organics'];
		$global_goods = $playerinfo['global_goods'];
		$global_energy = $playerinfo['global_energy'];
		$global_fighters = $playerinfo['global_fighters'];
		$global_torps = $playerinfo['global_torps'];
	}
	
	// Get updated planetary production values
	$production_string = $_GET['production_changes'];
	$prod_changes = explode("-",$production_string);
	
	foreach ($prod_changes as $change)
	{
		$planet_production = explode(",",$change);
		$planet_id = $planet_production[0];
		$prod_global = $planet_production[1];
		$prod_ore = $planet_production[2];
		$prod_organics = $planet_production[3];
		$prod_goods = $planet_production[4];
		$prod_energy = $planet_production[5];
		$prod_fighters = $planet_production[6];
		$prod_torp = $planet_production[7];
		
		// If prod_global is true, use the global production settings
		if ($prod_global == 'true')
		{
			$smcFunc['db_query']('','
				UPDATE {raw:db_planets}
				SET prod_ore = {int:prod_ore},
					prod_organics = {int:prod_organics},
					prod_goods = {int:prod_goods},
					prod_energy = {int:prod_energy},
					prod_fighters = {int:prod_fighters},
					prod_torp = {int:prod_torp},
					prod_global = "Y"
				WHERE planet_id = {int:planet_id}
				AND owner = {int:ship_id}',
				array(
					'db_planets' => $dbtables['planets'],
					'planet_id' => $planet_id,
					'ship_id' => $playerinfo['ship_id'],
					'prod_ore' => $global_ore,
					'prod_organics' => $global_organics,
					'prod_goods' => $global_goods,
					'prod_energy' => $global_energy,
					'prod_fighters' => $global_fighters,
					'prod_torp' => $global_torps,
				)
			);
		}
		else
		{
			$smcFunc['db_query']('','
				UPDATE {raw:db_planets}
				SET prod_ore = {int:prod_ore},
					prod_organics = {int:prod_organics},
					prod_goods = {int:prod_goods},
					prod_energy = {int:prod_energy},
					prod_fighters = {int:prod_fighters},
					prod_torp = {int:prod_torp},
					prod_global = "N"
				WHERE planet_id = {int:planet_id}
				AND owner = {int:ship_id}',
				array(
					'db_planets' => $dbtables['planets'],
					'planet_id' => $planet_id,
					'ship_id' => $playerinfo['ship_id'],
					'prod_ore' => $prod_ore,
					'prod_organics' => $prod_organics,
					'prod_goods' => $prod_goods,
					'prod_energy' => $prod_energy,
					'prod_fighters' => $prod_fighters,
					'prod_torp' => $prod_torp,
				)
			);
		}
	}

	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}
		WHERE owner = {int:ship_id}
		ORDER BY sector_id',
		array(
			'db_planets' => $dbtables['planets'],
			'ship_id' => $playerinfo['ship_id'],
		)
	);
	
	// Run a sanity check to ensure there are no illegal values in the players planet production values
	$i = 0;
	if ($smcFunc['db_num_rows']($result) > 0)
	{
		while ($row = $smcFunc['db_fetch_assoc']($result))
		{
			$planets[$i] = $row;
			$i++;
		}

		foreach ($planets as $planet)
		{
			if (empty($planet['name']))
				$planet['name'] = $txt['bnt_unnamed'];
			if ($planet['prod_ore'] < 0)
				$planet['prod_ore'] = 110;
			if ($planet['prod_organics'] < 0)
				$planet['prod_organics'] = 110;
			if ($planet['prod_goods'] < 0)
				$planet['prod_goods'] = 110;
			if ($planet['prod_energy'] < 0)
				$planet['prod_energy'] = 110;
			if ($planet['prod_fighters'] < 0)
				$planet['prod_fighters'] = 110;
			if ($planet['prod_torp'] < 0)
				$planet['prod_torp'] = 110;

			if ($planet['prod_ore'] + $planet['prod_organics'] + $planet['prod_goods'] + $planet['prod_energy'] + $planet['prod_fighters'] + $planet['prod_torp'] > 100)
			{
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_planets}
					SET prod_ore = {int:prod_ore},
						prod_organics = {int:prod_organics},
						prod_goods = {int:prod_goods},
						prod_energy = {int:prod_energy},
						prod_fighters = {int:prod_fighters},
						prod_torp = {int:prod_torp}
					WHERE planet_id = {int:planet_id}',
					array(
						'db_planets' => $dbtables['planets'],
						'planet_id' => $planet['planet_id'],
						'prod_ore' => $default_prod_ore,
						'prod_organics' => $default_prod_organics,
						'prod_goods' => $default_prod_goods,
						'prod_energy' => $default_prod_energy,
						'prod_fighters' => $default_prod_fighters,
						'prod_torp' => $default_prod_torp
					)
				);
			}
		}
	}
}

function ToggleSelling($planet_id)
{
	global $smcFunc;
	global $dbtables;
	
	$result = $smcFunc['db_query']('', '
		SELECT sells
		FROM {raw:db_planets}
		WHERE planet_id = {int:planet_id}',
		array(
			'db_planets' => $dbtables['planets'],
			'planet_id' => $planet_id
		)
	);
	$planetinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	if ($planetinfo['sells'] == "Y")
	{
		// Set planet to not sell
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_planets}
			SET sells = "N"
			WHERE planet_id = {int:planet_id}',
			array(
				'db_planets' => $dbtables['planets'],
				'planet_id' => $planet_id
			)
		);
		echo '<a class="planet_sells" href="#" title="Toggle Selling"><input type="hidden" value="', $planet_id ,'">', t_port_icon("sells_none"), '</a>';
	}
	else
	{
		// Set planet to sell
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_planets}
			SET sells = "Y"
			WHERE planet_id = {int:planet_id}',
			array(
				'db_planets' => $dbtables['planets'],
				'planet_id' => $planet_id
			)
		);
		echo '<a class="planet_sells" href="#" title="Toggle Selling"><input type="hidden" value="', $planet_id ,'">', t_port_icon("sells"), '</a>';
	}
}

function Real_Space_Move($destination,$playerinfo)
{
	global $smcFunc, $txt, $scripturl;
	global $dbtables;
	global $level_factor;

	$deg = pi() / 180;
	$destination = round(abs($destination));

	$result = $smcFunc['db_query']('', '
		SELECT angle1, angle2, distance
		FROM {raw:db_universe}
		WHERE sector_id = {int:sector_id}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector_id' => $playerinfo['sector']
		)
	);
	$start = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	$result = $smcFunc['db_query']('', '
		SELECT angle1, angle2, distance
		FROM {raw:db_universe}
		WHERE sector_id = {int:destination_id}',
		array(
			'db_universe' => $dbtables['universe'],
			'destination_id' => $destination
		)
	);
	$finish = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	$sa1 = $start['angle1'] * $deg;
	$sa2 = $start['angle2'] * $deg;
	$fa1 = $finish['angle1'] * $deg;
	$fa2 = $finish['angle2'] * $deg;
	$x = ($start['distance'] * sin($sa1) * cos($sa2)) - ($finish['distance'] * sin($fa1) * cos($fa2));
	$y = ($start['distance'] * sin($sa1) * sin($sa2)) - ($finish['distance'] * sin($fa1) * sin($fa2));
	$z = ($start['distance'] * cos($sa1)) - ($finish['distance'] * cos($fa1));
	$distance = round(sqrt(mypw($x, 2) + mypw($y, 2) + mypw($z, 2)));
	$shipspeed = mypw($level_factor, $playerinfo['engines']);
	$triptime = round($distance / $shipspeed);
	
	if ($triptime == 0 && $destination != $playerinfo['sector'])
		$triptime = 1;

	if ($destination == $playerinfo['sector'])
	{
		$triptime = 0;
		$energyscooped = 0;
	}

	if ($playerinfo['dev_fuelscoop'] == "Y")
		$energyscooped = $distance * 100;
	else
		$energyscooped = 0;
 
	if ($playerinfo['dev_fuelscoop'] == "Y" && $energyscooped == 0 && $triptime == 1)
		$energyscooped = 100;
	
	$free_power = NUM_ENERGY($playerinfo['power']) - $playerinfo['ship_energy'];

	// Amount of energy that can be stored is less than amount scooped amount scooped is set to what can be stored.
	if ($free_power < $energyscooped)
		$energyscooped = $free_power;

	// Make sure energyscooped is not null.
	if (!isset($energyscooped))
		$energyscooped = 0;

	// Make sure energy scooped not negative, or decimal
	if($energyscooped < 1)
		$energyscooped = 0;

	// check to see if already in that sector
	if ($destination == $playerinfo['sector'])
	{
		$triptime = 0;
		$energyscooped = 0;
	}

	if ($triptime > $playerinfo['turns'])
	{
		$txt['bnt_rs_movetime'] = str_replace("[triptime]", NUMBER($triptime), $txt['bnt_rs_movetime']);
		echo $txt['bnt_rs_movetime'], '<br><br>';
		echo $txt['bnt_rs_noturns'], '<br><br>';
		
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET cleared_defences = NULL
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);

		$retval = "BREAK";
	}
	else
	{
		// modified from traderoute.php
		// ********************************
		// ***** Sector Defense Check *****
		// ********************************
		$hostile = 0;

		$result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_sector_defence}
			WHERE sector_id = {int:dest}
			AND ship_id <> {int:ship_id}',
			array(
				'db_sector_defence' => $dbtables['sector_defence'],
				'dest' => $destination,
				'ship_id' => $playerinfo['ship_id']
			)
		);

		if ($smcFunc['db_num_rows']($result) > 0)
		{
			$fighters_owner = $smcFunc['db_fetch_assoc']($result);
			$smcFunc['db_free_result']($result);
			
			$result = $smcFunc['db_query']('', '
				SELECT *
				FROM {raw:db_ships}
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'ship_id' => $fighters_owner['ship_id']
				)
			);
			$nsfighters = $smcFunc['db_fetch_assoc']($result);
			$smcFunc['db_free_result']($result);
			
			if ($nsfighters['team'] != $playerinfo['team'] || $playerinfo['team'] == 0)
				$hostile = 1;
		}

		if ($hostile > 0 && $playerinfo['hull'] > $mine_hullsize)
		{
			$retval = "HOSTILE";
			$txt['bnt_pr_hostile'] = str_replace("[destination]", $destination, $txt['bnt_pr_hostile']);
			echo '<b>', $txt['bnt_pr_hostile'], '</b><br>';
		}
		else
		{
		$stamp = date("Y-m-d H-i-s");
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET last_login = NOW(),
				sector = {int:destination},
				ship_energy = ship_energy + {int:energyscooped},
				turns = turns - {int:triptime},
				turns_used = turns_used + {int:triptime}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'destination' => $destination,
				'energyscooped' => $energyscooped,
				'triptime' => $triptime,
				'ship_id' => $playerinfo['ship_id']
			)
		);
		
		$retval = "GO";	
		}
	}
	
	return($retval);
}

function RenamePlanet($planet_name,$planet_id)
{
	global $smcFunc, $txt, $user_info;
	global $dbtables;

	// Pull player info into $playerinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	// Update planet with new name.
	$smcFunc['db_query']('','
		UPDATE {raw:db_planets}
		SET name = {string:planet_name}
		WHERE planet_id = {int:planet_id}
		AND owner = {int:ship_id}',
		array(
			'db_planets' => $dbtables['planets'],
			'planet_id' => $planet_id,
			'ship_id' => $playerinfo['ship_id'],
			'planet_name' => $planet_name,
		)
	);
	
	echo '<span class="planet_rename" title="', $txt['bnt_pr_rename'], '">', $planet_name, '</span>';	
}

?>
